// pages/my_order/order.js
import {
  getOrderList
} from '../../api/orderList'
const Util = require('../../utils/util.js')
Page({
  data: {
    tabs: [{
        title: '全部',
      },
      {
        title: '待付款',
      },
      {
        title: '待发货',
      },
      {
        title: '待收货',
      },
      {
        title: '待评价',
      },
    ],
    orderList: [],
    page: 1,
    type: 0
  },
  // 时间函数封装
  timeChange(value) {
    value.map(item => {
      item.create_time = Util.formatTime(item.create_time)
      return item
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let {type} = options
    type = type || this.data.type
    this.setData({
      activeIndex:type
    })
    this.fetchOrderData(type)
  },
  async fetchOrderData(type) {
    let result = await getOrderList(this.data.page,type)
    // 时间转换
    result.data.forEach(item => {
      let newItem = this.timeChange(item)
      item = newItem
    })
    this.setData({
      orderList: result.data
    })
  },
  // 点击选项卡触发函数
  onTabClick(e) {
    // 参数 e 可以拿到选项卡下标值
    const index = e.detail.index
    this.setData({
      activeIndex: index,
      type: index
    })
    this.fetchOrderType()
  },
  // 获取订单分类数据
  async fetchOrderType() {
    let result = await getOrderList(this.data.page, this.data.type)
    // 时间转换
    result.data.forEach(item => {
      let newItem = this.timeChange(item)
      item = newItem
    })
    this.setData({
      orderList: result.data
    })
  },
})